package com.dhu.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhu.pojo.Bar;
import com.dhu.service.BarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
public class BarController {

    @Autowired
    private BarService barService;

    // 创建贴吧
    @RequestMapping("/createBar")
    public String createBar(Bar bar, MultipartFile file, HttpServletRequest req) throws IOException {
        JSONObject res = new JSONObject();
        // 储存文件
        String realPath = ResourceUtils.getURL("classpath:").getPath() + "static";
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName;
        file.transferTo(new File(realPath, newName));
        bar.setAvatar(newName);
        System.out.println(bar);
        if (barService.createBar(bar) == 1) {
            res.put("code", 200);
            res.put("message", "创建贴吧成功");
        } else {
            res.put("code", 403);
            res.put("message", "创建贴吧失败");
        }
        return res.toJSONString();
    }

    // 查询所有贴吧信息
    @RequestMapping("/selectAllBar")
    public String selectAllBar () {
        JSONObject res = new JSONObject();
        List<Bar> bars = barService.selectAllBar();
        //System.out.println(bars);
        if (bars != null) {
            res.put("code", 200);
            res.put("message", "成功查询所有贴吧信息");
            res.put("barList", bars);
        } else {
            res.put("code", 403);
            res.put("message", "查询所有贴吧信息失败");
        }
        return res.toJSONString();
    }

    // 获取用户关注的贴吧信息
    @GetMapping(value = "/followBarList", produces = "application/json;charset=utf-8")
    public String getFollowBarInfo(@RequestParam int id) {
        JSONObject res = new JSONObject();
        List<Bar> bars = barService.selectFollowBar(id);
        if (bars != null) {
            res.put("code", 200);
            res.put("message", "成功查询关注贴吧信息");
            res.put("barList", bars);
        } else {
            res.put("code", 200);
            res.put("message", "还没有关注吧");
        }
        return res.toJSONString();
    }

    // 根据id查询贴吧信息
    @GetMapping(value = "/getBarById", produces = "application/json;charset=utf-8")
    public String getBarById (int id) {
        JSONObject res = new JSONObject();
        Bar bar = barService.selectBarById(id);
        if (bar != null) {
            res.put("code", 200);
            res.put("message", "获取吧信息成功");
            res.put("barInfo", bar);
        } else {
            res.put("code", 403);
            res.put("message", "获取吧信息失败");
        }
        return res.toJSONString();
    }
}
